package com.ten.mall.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ten.mall.service.PrivilegeService;
import com.ten.mall.service.UserService;
import com.ten.mall.service.YaodianService;
import com.ten.mall.vo.PrivilegeUser;
import com.ten.mall.vo.User;
import com.ten.mall.vo.UserVo;
import com.ten.mall.vo.Yaodian;
@Controller
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private YaodianService yaodianService;
	
	@Autowired
	private PrivilegeService pService;
	@RequestMapping("/admin/userList")
	public String userList(@RequestParam(name="start",defaultValue="1") Integer start,
						   @RequestParam(name="size",defaultValue="10") Integer size,
							Model model,UserVo vo){
		vo.setState(1);
		Map<String,Object> map =userService.selectByPages(start, size, vo);
		List<User> list =(List<User>) map.get("list");
		model.addAttribute("list", list);
		Long mySize=(Long) map.get("total");
		int total = mySize.intValue()/10+1;
		model.addAttribute("page", start);
		model.addAttribute("total", total);
		return "jsp/user";
	}
	@RequestMapping("/admin/toAdd")
	public String toAdd(String id,Model model){
		if(StringUtils.isNotBlank(id)){
			User u =userService.selectById(id);
			model.addAttribute("user", u);
		}
		return "jsp/userAdd";
	}
	@RequestMapping("/admin/userAdd")
	public String addUser(User u){
		if(StringUtils.isNotBlank(u.getPassword())){
			String pass =DigestUtils.md5DigestAsHex(u.getPassword().getBytes());
			u.setPassword(pass);
		}
		if(StringUtils.isNotBlank(u.getId())){
			userService.updateUser(u);
		}else{
			userService.insertUser(u);
		}
		return "redirect:/admin/userList";
	}
	@RequestMapping("/admin/userDel")
	@ResponseBody
	public String userDel(String id){
		String ss="ok";
		try {
			userService.delUser(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ss="error";
		}
		return ss;
	}
	
	@RequestMapping("/toLogin")
	public String toLogin(){
		return "jsp/login";
	}
	
	@RequestMapping("/login")
	public String login(String username,String password,
						HttpServletRequest res){
		HttpSession session =res.getSession();
		String rs="jsp/login";
			if(StringUtils.isNotBlank(username)&&StringUtils.isNotBlank(password)){
				User u =userService.login(username, password);
				if(u!=null){
					//res.setAttribute(name, o);
					session.setAttribute("user", u);
					List<PrivilegeUser> pList =pService.selectMyPrilige(u.getId());
					session.setAttribute("pList", pList);
					rs="redirect:/admin";
				}else{
					res.setAttribute("errorMsg","用户名或密码错误!");
				}
			}else{
				res.setAttribute("errorMsg","用户名或密码不能为空!");
			}
			
		return rs;
	}
	@RequestMapping("/loginout")
	public String loginOut(HttpServletRequest rs){
		rs.getSession().removeAttribute("user");
		return "redirect:/toLogin";
	}
	@RequestMapping("/phone/doctor")
	public String toDoctor(){
		return "phone/doctor";
	}
	/**
	 * 根据医生的id查询病人
	 * @param start
	 * @param size
	 * @param vo
	 * @param res
	 * @return
	 */
	@RequestMapping("/phone/doctorUser")
	public String doctorUser(@RequestParam(name="start",defaultValue="1") Integer start,
			   				 @RequestParam(name="size",defaultValue="10") Integer size,
			   				 UserVo vo,HttpServletRequest res){
		User u =(User) res.getSession().getAttribute("u");
		vo.setDoctorId(u.getId());
		Map<String,Object> map =userService.selectByPages(start, size, vo);
		List<User> list =(List<User>) map.get("list");
		res.setAttribute("list", list);
		return "phone/doctor_huzhe";
	}
	
	/**
	 * 店员列表
	 * @param start
	 * @param size
	 * @param name
	 * @param res
	 * @return
	 */
	@RequestMapping("/admin/dianyuanList")
	public String dianyuanList(@RequestParam(name="start",defaultValue="1") Integer start,
			   				  @RequestParam(name="size",defaultValue="10") Integer size,
			   				  String name,HttpServletRequest res){
		UserVo vo =new UserVo();
		vo.setRoleName("店员");
		if(StringUtils.isNotBlank(name)){
			vo.setUsername(name);
		}
		Map<String,Object> map=userService.selectByPages(start, size, vo);
		List<User> list =(List<User>) map.get("list");
		res.setAttribute("list", list);
		Long mySize=(Long) map.get("total");
		int total = mySize.intValue()/10+1;
		res.setAttribute("page", start);
		res.setAttribute("total", total);
		return "jsp/dianyuanList";
	}
	
	/**
	 * 增加店员
	 * @param id
	 * @param res
	 * @return
	 */
	@RequestMapping("/admin/dianyuanAdd")
	public String dianyuanAdd(String id,HttpServletRequest res){
		User u =userService.selectById(id);
		List<Yaodian> doctorlist =yaodianService.selectAll(null);
		res.setAttribute("doctor", u);
		res.setAttribute("doctorlist", doctorlist);
		return "jsp/dianyuanAdd";
	}
	/**
	 * 保存店员
	 * @param u
	 * @param res
	 * @return
	 */
	@RequestMapping("/admin/dianyuanSave")
	public String dianyuanSave(User u,HttpServletRequest res){
		userService.updateUser(u);
		return "redirect:/admin/dianyuanList";
	}
	/**
	 * 查询所有的医生
	 * @return
	 */
	@RequestMapping("/admin/listDoctor")
	@ResponseBody
	public List<Map<String,String>> listDoctor(String name){
		List<Map<String,String>> map =userService.selectYishen(name);
		return  map;
	}
	/**
	 * 查询医生销量
	 * @return
	 */
	@RequestMapping("/phone/mySearch")
	public String toDoctorSearch(String doctorId,HttpServletRequest res){
		res.setAttribute("doctorId", doctorId);
		return "phone/my_search";
				
	}
	@RequestMapping("phone/yaodianSearch")
	public String toYaodianSearch(String yaoId,HttpServletRequest res){
		res.setAttribute("yaoId", yaoId);
		return "phone/yaodian_search";
				
	}
	
	@RequestMapping("/admin/xiugai")
	public String xiugai(User u,String rePass,HttpServletRequest res){
		String rs ="jsp/rePass";
		if(StringUtils.isNotBlank(u.getUsername())&&StringUtils.isNotBlank(u.getPassword())){
			User user =userService.login(u.getUsername(), u.getPassword());
			if(user!=null){
				String str =DigestUtils.md5DigestAsHex(rePass.getBytes());
				user.setPassword(str);
				userService.updateUser(user);
				rs ="redirect:/admin";
				}else{
					res.setAttribute("errorMsg", "旧密码输入错误!");
				}
			}else{
				res.setAttribute("errorMsg", "新旧密码不能为空!");
			}
		return rs;
	}
}
